package persistent.prestige.platform.base.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
 * @Title:RegxUtils.java
 * @Package persistent.prestige.util
 * @Description TODO
 * @author prestigeding
 * @date 2012-12-16 下午02:44:50
 * @version V1.0
 */

public class RegxUtils {

	public static final String replace(String regx, String str,
			boolean caseInsensitive) {
		Pattern pattern = null;
		if (caseInsensitive) {
			pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
		} else {
			pattern = Pattern.compile(regx);
		}

		Matcher matcher = pattern.matcher(str);
		return matcher.replaceAll("");

	}

	/**
	 * 判断是否是邮件
	 * 
	 * @param email
	 * @return
	 */
	public static final boolean isEmail(String email) {
		Pattern emailer = Pattern
				.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		if (StringUtils.isBlank(email))
			return false;
		email = email.toLowerCase();
		if (email.endsWith(".con"))
			return false;
		if (email.endsWith(".cm"))
			return false;
		if (email.endsWith("@gmial.com"))
			return false;
		if (email.endsWith("@gamil.com"))
			return false;
		if (email.endsWith("@gmai.com"))
			return false;
		return emailer.matcher(email).matches();
	}

	public static final String replace(String regx, String str, String newStr,
			boolean caseInsensitive) {
		if (StringUtils.isEmpty(newStr)) {
			newStr = "";
		}
		Pattern pattern = null;
		if (caseInsensitive) {
			pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
		} else {
			pattern = Pattern.compile(regx);
		}

		Matcher matcher = pattern.matcher(str);
		return matcher.replaceAll(newStr);

	}
	
	/** 
	 * 去掉空白字符
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}
	
	public static boolean isNumber(String number) {
		Pattern p = Pattern.compile("^-?(?:[0-9]+|[0-9]{1,3}(?:,[0-9]{3})+)?(?:\\.[0-9]+)?$");
		Matcher matcher = p.matcher(number);
		return matcher.matches();
	}

	public static void main(String[] args) {

		System.out.println(replace("", "sadfdfdf\"dfdf\"dsfd 'ad'", false));

		System.out.println("sadfdfdf\"dfdf\"dsfd 'ad'".replaceAll("\"",
				"&quot;"));

		/*
		 * String regx = "[0-9a-fA-F\\-]*";
		 * 
		 * Pattern pattern = Pattern.compile(regx); Matcher matcher =
		 * pattern.matcher("8a80828f-3c61fbc7013c-664a01360086");
		 * System.out.println(matcher.matches());
		 */
		// String sql = "select * from t_tab where 1=1 and mo.name=:name and
		// exists(select 1 from t_exp t2 order by t2.id desc) order by mo.name
		// desc";
		//
		// //String regx = "\\s*Order\\s*by[/w\\.\\s^\\)]*";
		//
		// String regx = "(\\sorder\\s*by)(/w\\.\\s)(\\()?";
		//
		// // String regx = "1\\.test";
		// //
		// // String sql = "13431.test";
		//
		// System.out.println(sql);
		// System.out.println(replace(regx, sql, true));
	}

}
